package com.miaosmart.service.impl;

import com.miaosmart.service.MailService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

    private final JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${project.name}")
    private String projectName;

    @Override
    public void sendVerificationCode(String to, String code) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(projectName + " - 验证码");

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime expireTime = now.plusMinutes(5);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // HTML 邮件模板
        String htmlContent = String.format("""
            <div style="background-color: #f5f5f5; padding: 20px;">
                <div style="max-width: 600px; margin: 0 auto; background-color: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
                    <div style="text-align: center; margin-bottom: 20px;">
                        <h1 style="color: #ff6700; margin: 0;">%s</h1>
            
                    </div>
                    <div style="border-top: 1px solid #eee; padding: 20px 0;">
                        <p style="font-size: 14px; color: #333; margin-bottom: 10px;">亲爱的用户：</p>
                        <p style="font-size: 14px; color: #333; line-height: 1.6;">
                            您正在进行邮箱验证，请在验证码输入框中输入以下验证码：
                        </p>
                        <div style="background-color: #f8f8f8; padding: 15px; border-radius: 5px; text-align: center; margin: 15px 0;">
                            <span style="font-size: 24px; font-weight: bold; color: #ff6700; letter-spacing: 5px;">%s</span>
                        </div>
                        <p style="font-size: 14px; color: #666; margin-bottom: 10px;">
                            • 验证码有效期：<span style="color: #ff6700;">5分钟</span><br>
                            • 发送时间：%s<br>
                            • 过期时间：%s
                        </p>
                        <p style="font-size: 14px; color: #666; line-height: 1.6;">
                            为了保障您的账户安全，请勿向他人泄露验证码信息。如非本人操作，请忽略此邮件。
                        </p>
                    </div>
                    <div style="border-top: 1px solid #eee; padding-top: 20px; text-align: center;">
                        <p style="font-size: 12px; color: #999; margin: 0;">
                            本邮件由系统自动发送，请勿直接回复。<br>
                            如有问题请联系客服或访问帮助中心。
                        </p>
                    </div>
                </div>
            </div>
            """, 
            projectName,
            code,
            now.format(formatter),
            expireTime.format(formatter)
        );

        helper.setText(htmlContent, true);
        mailSender.send(message);
    }
} 